﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using StackExchange.Redis;

namespace Simple.Common.Cache
{
    public partial class RedisService
    {
        #region hash
        /// <summary>
        /// 判断该字段是否存在 hash 中
        /// </summary>
        /// <param name="key"></param>
        /// <param name="hashField"></param>
        /// <returns></returns>
        public async Task<bool> HashExistsAsync(string key, string hashField) => await _database.HashExistsAsync(key, hashField);

        /// <summary>
        /// 从 hash 中移除指定字段
        /// </summary>
        /// <param name="key"></param>
        /// <param name="hashField"></param>
        /// <returns></returns>
        public async Task<bool> HashDeleteAsync(string key, string hashField) => await _database.HashDeleteAsync(key, hashField);
        /// <summary>
        /// 从 hash 中移除多个指定字段
        /// </summary>
        /// <param name="key"></param>
        /// <param name="hashField"></param>
        /// <returns></returns>
        public async Task<long> HashDeleteAsync(string key, IEnumerable<RedisValue> hashField) => await _database.HashDeleteAsync(key, hashField.ToArray());

        /// <summary>
        /// 在 hash 中保存或修改一个值   字符类型
        /// set or update the HashValue for string key 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="hashField"></param>
        /// <param name="value"></param>
        /// <param name="expireTimeSpan">过期时间,可空</param>
        /// <returns></returns>
        public async Task<bool> HashSetAsync(string key, string hashField, string value, TimeSpan? expireTimeSpan = null)
        {
            expireTimeSpan = GetTimeSpan(expireTimeSpan);
            return (await _database.HashSetAsync(key, hashField, value) && _database.KeyExpire(key, expireTimeSpan));
        }

        /// <summary>
        /// 保存一个字符串类型集合  
        /// </summary>
        /// <param name="key">Redis Key</param>
        /// <param name="list">数据集合</param>
        /// <param name="getFiledKey">字段key</param>
        /// <param name="expireTimeSpan"></param>
        public async Task<bool> HashSetListAsync(string key, IEnumerable<string> list, Func<string, string> getFiledKey, TimeSpan? expireTimeSpan = null)
        {
            expireTimeSpan = GetTimeSpan(expireTimeSpan);
            await _database.HashSetAsync(key, list.Select(item => new HashEntry(getFiledKey(item), item)).ToArray());
            return await ExpireAsync(key, expireTimeSpan);
        }
        /// <summary>
        /// 保存或修改一个hash对象（序列化）
        /// </summary>
        /// <param name="key"></param>
        /// <param name="hashField"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public async Task<bool> HashSetAsync<T>(string key, string hashField, T value, TimeSpan? span = null, int db = -1) where T : class
        {
            var json = JsonConvert.SerializeObject(value);
            return await _database.HashSetAsync(key, hashField, json)&& await ExpireAsync(key);
        }

        #endregion
    }
}
